#!/bin/sh
### BEGIN INIT INFO
# Provides:		shellinaboxd
# Required-Start:	$remote_fs $syslog $networking
# Required-Stop:	$remote_fs $syslog
# Default-Start:	2 3 4 5
# Default-Stop:		1
# Short-Description:	Shell In A Box server
### END INIT INFO
#

SHELLINABOX_PORT=4200
SHELLINABOX_ARGS="--user=root --disable-ssl --localhost-only"
NO_START=0

test ! -r /etc/default/shellinabox || . /etc/default/shellinabox

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/shellinaboxd
NAME=shellinabox
DESC="Shell In A Box server"
PIDFILE=/var/run/shellinabox.pid
current_second=0
while [ ! -e /dev/log ]; do
    if [ "$current_second" -ge "5" ]; then
        break # This will exit the loop
    fi
    sleep 1
    ((current_second++))
done
sleep 1

set -e

test "$NO_START" = "0" || exit 0
test -x "$DAEMON" || exit 0

case "$1" in
	start)
		echo -n "Starting $DESC: "
		start-stop-daemon -S -p $PIDFILE -x "$DAEMON" -- --port="$SHELLINABOX_PORT" --background="$PIDFILE" $SHELLINABOX_ARGS
		echo "$NAME."
		;;
	stop)
		echo -n "Stopping $DESC: "
		start-stop-daemon -K -x "$DAEMON" -p $PIDFILE
		echo "$NAME."
		;;
	restart|reload)
		$0 stop
		$0 start
		;;
	*)
		echo "Usage: $0 {start|stop|restart}"
		exit 1
		;;
esac

exit 0

